package com.ohs.bo.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.ohs.bll.service.LocationService;
import com.ohs.bll.service.VideoService;
import com.ohs.dal.model.Location;
import com.ohs.dal.model.Tag;
import com.ohs.dal.model.Video;
import com.ohs.fo.action.AbstractAction;

public class HomeAction extends AbstractAction {
	private LocationService locationService;
	private VideoService vidService;
	
	private List<Tag> tags;
	private Video video;
	private String externalId;
	private String name;
	private String description;
	private String thumbnail;
	private String length;
	private String selectedTag;
	
	public String show() throws Exception {
		return "success";
	}

	public String gen() throws Exception {
		List<Location> locations = locationService.getAllLocation();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/xml");
        StringBuffer output = new StringBuffer();
        
        for (Location location : locations) {
        	output.append("<url>");
        	output.append("<loc>http://www.dathenkhambenh.com/home/detail/" + location.getLocationId() + "</loc>");
        	output.append("</url>");
        }
        
        List<Video> videos = vidService.listAll();
        for (Video video : videos) {
        	output.append("<url>");
        	output.append("<loc>http://www.dathenkhambenh.com/video/" + video.getId() + "</loc>");
        	output.append("</url>");
        }
        PrintWriter writer = response.getWriter();
        writer.write(output.toString());
			
		return null;
	}

	public String addVid() throws Exception {
		tags = vidService.getTags();
		return "success";
	}
	
	public String addVidSubmit() throws Exception {
		Set<Tag> vidTags = new HashSet<Tag>();
		vidTags.add(vidService.getTag(new Long(selectedTag)));
		Video newVid = new Video(externalId, name, description, thumbnail, length);
		newVid.setTags(vidTags);
		newVid.setCreatedDate(new Date());
		vidService.add(newVid);
		return "success";
	}
	
	public LocationService getLocationService() {
		return locationService;
	}

	public void setLocationService(LocationService locationService) {
		this.locationService = locationService;
	}

	public VideoService getVidService() {
		return vidService;
	}

	public void setVidService(VideoService vidService) {
		this.vidService = vidService;
	}

	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}

	public Video getVideo() {
		return video;
	}

	public void setVideo(Video video) {
		this.video = video;
	}

	public String getExternalId() {
		return externalId;
	}

	public void setExternalId(String externalId) {
		this.externalId = externalId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getThumbnail() {
		return thumbnail;
	}

	public void setThumbnail(String thumbnail) {
		this.thumbnail = thumbnail;
	}

	public String getLength() {
		return length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	public String getSelectedTag() {
		return selectedTag;
	}

	public void setSelectedTag(String selectedTag) {
		this.selectedTag = selectedTag;
	}
	
	
}